package com.itheima.zxingdemo;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.SurfaceOps;
import ohos.app.Context;

import java.util.Optional;


public class ScanAbility extends Ability implements SurfaceOps.Callback{

    private Context mContext= null;
    private SurfaceProvider surfaceProvider;
    private Text tResult;
    private DirectionalLayout  finderView,surfaceView;
//    private FinderComponent finderComponent;
    private boolean success = false;

    public static final int VIDEO_WIDTH = 640;
    public static final int VIDEO_HEIGHT = 480;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_scan);
        // 查找布局中组件
        mContext = this;
//        cameraManager = new CameraManager(getApplicationContext(),this);
        initView();
    }

    private void initView() {
        tResult = (Text) findComponentById(ResourceTable.Id_result);
        finderView= (DirectionalLayout) findComponentById(ResourceTable.Id_finc_view);

//        DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT);
//        finderComponent = new FinderComponent(this);
//        finderComponent.setLayoutConfig(config);
//        finderView.addComponent(finderComponent);

//        surfaceView= (DirectionalLayout) findComponentById(ResourceTable.Id_surface_view);
//        //SurfaceProvider，用于播放摄像头画面
//        config.width = VIDEO_WIDTH;
//        config.height = VIDEO_HEIGHT;
//        config.alignment = LayoutAlignment.HORIZONTAL_CENTER;
//        surfaceProvider = new SurfaceProvider(this);
//        surfaceProvider.setLayoutConfig(config);
//        surfaceView.addComponent(surfaceProvider);
//        surfaceProvider = (SurfaceProvider) findComponentById(ResourceTable.Id_surfaceProvider);
        Optional<SurfaceOps> ops = surfaceProvider.getSurfaceOps();
        ops.get().addCallback(this);
        surfaceProvider.pinToZTop(true);
        tResult.setVisibility(Component.INVISIBLE);

    }

    @Override
    protected void onActive() {
        super.onActive();
//        cameraManager.startLoopTrigger();
//        finderComponent.setCameraManager(cameraManager);
//        finderComponent.animStart();

    }

    @Override
    protected void onInactive() {
        super.onInactive();
//        cameraManager.stopTrigger();
//        if (finderComponent != null){
//            finderComponent.stopAnim();
//        }
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
//        cameraManager.stop();
    }

    /*surface callback*/
    @Override
    public void surfaceCreated(SurfaceOps surfaceOps) {
//        cameraManager.creatCamera(new CameraStateImpl(cameraManager,surfaceProvider.getSurfaceOps().get().getSurface(),getLayoutParams().width,getLayoutParams().height));
    }

    @Override
    public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceOps surfaceOps) {

    }
    /*surface callback*/

//    @Override
//    public void onDecodeSuccess(Result result) {
//        super.onDecodeSuccess(result);
//        success = true;
//        tResult.setText(result.getText());
//        PixelMap pixelMap = getSuccessPixelMap(result);
//        tResult.setVisibility(Component.VISIBLE);
//        cameraManager.stopTrigger();
//        finderComponent.drawResultBitmap(pixelMap);
//    }

//    private PixelMap getSuccessPixelMap(Result result) {
//        return resultMap;
//    }
//
//    public void triContinue(){
//        success = false;
//        tResult.setText("");
//        tResult.setVisibility(Component.INVISIBLE);
//        cameraManager.startLoopTrigger();
//        finderComponent.drawViewfinder();
//
//    }



//    @Override
//    public void onDecodeFail() {
//        super.onDecodeFail();
//        cameraManager.startCallbackHandler();
//    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
//        switch (keyCode) {
//            case KeyEvent.KEY_BACK:
//                if (success) {
//                    InnerEvent event = InnerEvent.get(ScanAbilityHandler.SHOURESULT);
//                    mEventHandler.sendEvent(event,2000);
//                    return true;
//                }
//                break;
//        }
//        return super.onKeyDown(keyCode, keyEvent);
//    }
}
